package cn.hnu.qq.frame;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import cn.hnu.qq.client.controller.CreateCrowdController;
import cn.hnu.qq.client.controller.ListenServerMessageThread;
import cn.hnu.qq.client.event.MessageEvent;
import cn.hnu.qq.client.listener.MessageListener;
import cn.hnu.qq.entity.message.Message;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class CreateCrowdDialog extends JDialog implements ActionListener,MessageListener{

	private final JPanel contentPanel = new JPanel(){
		@Override
		protected void paintComponent(Graphics g) {
			Image image = null;
			try {
				image = ImageIO.read(new File("src/main/java/image/02.jpg"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			};
					
			g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

		}
	};
	private JTextField textField;

	private	CreateCrowdController controller = CreateCrowdController.getInstance();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			CreateCrowdDialog dialog = new CreateCrowdDialog();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public CreateCrowdDialog() {
		init();
	}

	public CreateCrowdDialog(Dialog owner, boolean modal) {
		super(owner, modal);
		init();
	}

	public CreateCrowdDialog(Frame owner, boolean modal) {
		super(owner, modal);
		init();
	}

	private void init() {
		try {
			setIconImage(ImageIO.read(new File("src/main/java/image/xiaohui.jpg")));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		setBounds(100, 100, 313, 185);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(new BorderLayout(0, 0));
		{
			JLabel lblNewLabel = new JLabel("         ");
			contentPanel.add(lblNewLabel, BorderLayout.NORTH);
		}
		{
			JLabel lblNewLabel_1 = new JLabel("         ");
			contentPanel.add(lblNewLabel_1, BorderLayout.WEST);
		}
		{
			JPanel panel = new JPanel(){
				@Override
				protected void paintComponent(Graphics g) {
					Image image = null;
					try {
						image = ImageIO.read(new File("src/main/java/image/02.jpg"));
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					};
							
					g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

				}
			};
			contentPanel.add(panel, BorderLayout.CENTER);
			panel.setLayout(new FormLayout(new ColumnSpec[] {
					FormFactory.RELATED_GAP_COLSPEC,
					FormFactory.DEFAULT_COLSPEC,
					FormFactory.RELATED_GAP_COLSPEC,
					FormFactory.DEFAULT_COLSPEC,
					FormFactory.RELATED_GAP_COLSPEC,
					FormFactory.DEFAULT_COLSPEC,
					FormFactory.RELATED_GAP_COLSPEC,
					ColumnSpec.decode("default:grow"),},
				new RowSpec[] {
					FormFactory.RELATED_GAP_ROWSPEC,
					FormFactory.DEFAULT_ROWSPEC,
					FormFactory.RELATED_GAP_ROWSPEC,
					FormFactory.DEFAULT_ROWSPEC,}));
			{
				JLabel lblCrowdName = new JLabel("crowd name:");
				panel.add(lblCrowdName, "4, 4");
			}
			{
				textField = new JTextField();
				panel.add(textField, "8, 4, left, default");
				textField.setColumns(15);
			}
		}
		{
			JPanel buttonPane = new JPanel(){
				@Override
				protected void paintComponent(Graphics g) {
					Image image = null;
					try {
						image = ImageIO.read(new File("src/main/java/image/02.jpg"));
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					};
							
					g.drawImage(image, 0, 0, this.getWidth(), this.getHeight(), this);

				}
			};
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				okButton.addActionListener(this);
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				cancelButton.addActionListener(this);
				buttonPane.add(cancelButton);
			}
		}
		this.setResizable(false);
		ListenServerMessageThread.getInstance().addMessageListener(this);
	}

	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand().equals("OK")){
			String crowdName = textField.getText();
			if(crowdName == null || crowdName.equals("")){
				JOptionPane.showMessageDialog(this, "请输入群组名");
				return;
			}
			controller.createCrowd(crowdName);
			
		}
	}

	public void messageArrived(MessageEvent event) {
		Message message = event.getSource();
		switch(message.getMessageType()){
			case CROWD_EXIT:
				JOptionPane.showMessageDialog(this, "该群组已经存在");
			break;
			case SUCCESS_CREATE_CROWD:
				JOptionPane.showMessageDialog(this, "新建群组成功");
				this.dispose();
			break;
		}
	}

}
